import { Body, Inject, Post, Provide, ALL } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { GoodsInfoEntity } from '../../entity/info';
import { GoodsInfoService } from '../../service/info';
import { UserInfoEntity } from '../../../user/entity/info';

/**
 * 商品
 */
@Provide()
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: GoodsInfoEntity,
  service: GoodsInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['title', 'subTitle', 'keywords', 'a.id'],
    fieldEq: ['status'],
    select: ['a.*, b.nickname, b.headimgurl, b.phone'],
    leftJoin: [
      {
        alias: 'b',
        condition: 'a.userId = b.id',
        entity: UserInfoEntity,
      }
    ],
    // // 增加其他条件
    // where: async (ctx: Context) => {
    //   return [
    //     // 状态为上架
    //     ['a.status = :status', { status: 1 }],
    //   ];
    // },
  },
})
export class AdminGoodsInfoController extends BaseController {
  @Inject()
  goodsInfoService: GoodsInfoService;

  @Post('/search')
  async search(@Body(ALL) query: any) {
    return this.ok(await this.goodsInfoService.search(query));
  }
}
